package com.lj.demo.eduservice.controller.front;

import com.lj.demo.commonutils.JwtUtils;
import com.lj.demo.commonutils.R;
import com.lj.demo.commonutils.daoVo.CourseWebVoOne;
import com.lj.demo.eduservice.client.OrderClient;
import com.lj.demo.eduservice.entity.CourseInfoForm;
import com.lj.demo.eduservice.entity.CoursePublishVo;
import com.lj.demo.eduservice.entity.vo.ChapterVo;
import com.lj.demo.eduservice.entity.vo.CourseWebVo;
import com.lj.demo.eduservice.service.EduChapterService;
import com.lj.demo.eduservice.service.EduCourseService;
import com.lj.demo.servicebase.exception.AException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: liujiang
 * @Date: 2020/12/28/21:04
 * @Description:
 */
@Api(description = "前台课程页面")
@RequestMapping("/eduservice/courseFront")
@RestController
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "根据id查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(@PathVariable String courseId){

        //查询课程信息和讲师信息
        CourseWebVo courseWebVo =courseService.selectInfoWebById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.findChapterById(courseId);
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList);
    }

    @ApiOperation("根据课程id获取课程信息")
    @GetMapping("getDto/{courseId}")
    public CourseWebVoOne getDto(@PathVariable String courseId){
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        CourseWebVoOne courseInfoFormVo = new CourseWebVoOne();
        BeanUtils.copyProperties(courseWebVo,courseInfoFormVo);
        return courseInfoFormVo;
    }

    @ApiOperation("根据id查询课程详情信息")
    @GetMapping("getCourseINfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId,
                           HttpServletRequest request){
        //根据课程查询课程基本信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);

        //查询课程里面大纲数
        List<ChapterVo> chapterById = chapterService.findChapterById(courseId);

        //远程调用，判断是否被购买
        Boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("courseFrontInfo",courseWebVo).data("chapterVoList",chapterById).data("isbuy",buyCourse);
    }






}
